/*
 * ©2023 JOY-HUBS.COM
 */

package com.joyhubs.base.advice;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import lombok.extern.slf4j.Slf4j;

import com.joyhubs.base.config.JoyWebAutoConfiguration;
import com.joyhubs.base.entity.ContentBody;
import com.joyhubs.base.exception.BizException;

/**
 * 全局异常处理
 * <p>
 * Problem Details for HTTP APIs, see <a href="https://www.rfc-editor.org/rfc/rfc7807">rfc7807</a>
 * <p>
 * 1.数据验证异常
 *
 * @author zhaozw
 * @date 2019-09-10
 * @date 2023-10-18
 */
@Slf4j
@ControllerAdvice
@ConditionalOnBean(JoyWebAutoConfiguration.class)
public class AppExceptionHandler extends ResponseEntityExceptionHandler {
    
    /**
     * 处理自定义的业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public ContentBody bizExceptionHandler(BizException e) {
        log.error("BizException [{}]{}", e.getErrorCode(), e.getErrorMsg());
        return ContentBody.error(e.getErrorCode(), e.getErrorMsg());
    }
    
}
